package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.PwdDTO;
import com.woniuxy.mesboot.dto.UserDTO;
import com.woniuxy.mesboot.entity.SysUser;
import com.woniuxy.mesboot.service.EmpService;
import com.woniuxy.mesboot.service.MenuService;
import com.woniuxy.mesboot.service.UserService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.EmpVO;
import com.woniuxy.mesboot.vo.MenuVO;
import com.woniuxy.mesboot.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/18 12:12
 * @description：TODO
 */
@RestController
@RequestMapping("users")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @Resource
    private EmpService empService;

    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * @Description 登录
     * @Return
     * @Author liSir
     * @Date Create by 2023/5/24 11:23
     */
//    @PostMapping("login")
    public Result login(@RequestBody UserDTO user) {
        UserVO userVO = userService.selectByNameAndPwd(user);
        if (userVO != null && userVO.getStatus() == 0) {
            List<MenuVO> menuVOS = menuService.selectMenuByName(user.getNickName(), 0);
            return Result.ok("登陆成功").data("user", userVO).data("menuList",menuVOS);
        } else if (userVO != null && userVO.getStatus() == 1) {
            return Result.error("用户已被锁定，请联系管理员解锁");
        } else {
            return Result.error("用户名或密码错误");
        }
    }

    /**
     * @Description 新增用户
     * @Return
     * @Author liSir
     * @Date Create by 2023/5/24 11:23
     */
    @PostMapping()
    public Result add(@RequestBody SysUser user) {
        UserVO userVO = userService.selectByName(user.getUserName());
        if (userVO != null) {
            return Result.error("用户名重复");
        } else {
            userService.insertSelective(user);
            return Result.ok("添加成功");
        }
    }

    /**
     * @Description 删除用户
     * @Return
     * @Author liSir
     * @Date Create by 2023/5/24 11:22
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        SysUser sysUser = userService.selectByPrimaryKey(id);
        sysUser.setStatus(1);
        int i = userService.updateByPrimaryKeySelective(sysUser);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * @Description 修改用户信息
     * @Return
     * @Author liSir
     * @Date Create by 2023/5/24 11:22
     */
    @PutMapping()
    public Result update(@RequestBody SysUser user) {
        UserVO userVO = userService.selectByName(user.getUserName());
        if (userVO != null && userVO.getUserId() != user.getUserId()) {
            return Result.error("用户名重复");
        } else {
            userService.updateByPrimaryKeySelective(user);
            return Result.ok("修改成功");
        }
    }

    @PutMapping("/pwd")
    public Result updatePwd(@RequestBody PwdDTO pwdDTO) {
        SysUser user = new SysUser();
        user.setUserId(pwdDTO.getUserId());
        user.setPassword(passwordEncoder.encode(pwdDTO.getNewPwd()));
        int i = userService.updateByPrimaryKeySelective(user);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/judgment")
    public Result judgmentPwd(@RequestBody UserDTO userDTO) {
        UserVO userVO = userService.selectByName(userDTO.getUserName());
        String oldPwd = userVO.getPassword();
        System.out.println(oldPwd);
        System.out.println(userDTO.getPassword());
        boolean matches = passwordEncoder.matches(userDTO.getPassword(), oldPwd);
        System.out.println(matches);
        if (matches) {
            return Result.ok("原密码正确");
        } else {
            return Result.error("原密码错误");
        }
    }
    /**
     * @Description 按用户名查询
     * @Return
     * @Author liSir
     * @Date Create by 2023/5/24 11:22
     */
    @GetMapping("queryByName")
    public Result queryByName(@RequestBody UserDTO user) {
        UserVO userVO = userService.selectByName(user.getUserName());
        if (userVO != null) {
            return Result.error("用户名重复");
        } else {
            return Result.ok();
        }
    }

    /**
     * @Description 按id查询
     * @Return
     * @Author liSir
     * @Date Create by 2023/5/24 11:22
     */
    @PostMapping("queryInfo")
    public Result queryById(@RequestBody UserDTO dto) {
        SysUser sysUser = userService.selectByPrimaryKey(dto.getUserId());
        EmpVO empVO = empService.selectByWorkNo(dto.getWorkNo());
        if (sysUser != null) {
            return Result.ok().data("user", sysUser).data("emp", empVO);
        }
        return Result.error("未找到用户信息");
    }

    /**
     * @Description 查询全部
     * @Return
     * @Author liSir
     * @Date Create by 2023/5/24 11:21
     */
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody UserDTO user) {
        PageHelper.startPage(user.getCurrentPage(), user.getPageSize(), true);
        List<UserVO> users = userService.selectAll(user);
        PageInfo<UserVO> pageInfo = new PageInfo<>(users);
        return Result.ok().data("list", users).data("total", pageInfo.getTotal());
    }
}
